package com.cedancp.hometrade;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.cedancp.hometrade.DetallePublicacion.AddRemFav;

import library.DatabaseHandler;
import library.UserFunctions;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EditCuenta extends Activity {
	
	TextView txt_nombre;
	TextView txt_email;
	TextView txt_telefono;
	TextView txt_password;
	TextView txt_passwordNueva;
	CheckBox chk_cambiarPass;
	Button btn_guardar;
	DatabaseHandler db;
	HashMap<String, String> user=new HashMap<String, String>();
	ProgressDialog pDialogo;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_cuenta);
		txt_nombre=(TextView) findViewById(R.id.txt_nombreEdit);
		txt_email=(TextView) findViewById(R.id.txt_emailEdit);
		txt_telefono=(TextView) findViewById(R.id.txt_telefonoEdit);
		txt_password=(TextView) findViewById(R.id.txt_passwordEdit);
		txt_passwordNueva=(TextView) findViewById(R.id.txt_passwordNueva);
		chk_cambiarPass=(CheckBox) findViewById(R.id.chk_cambiarPass);
		btn_guardar=(Button) findViewById(R.id.btn_guardarUsuario);
		db=new DatabaseHandler(getApplicationContext());
		user=db.getUserDetails();
		txt_nombre.setText(user.get("nombre"));
		txt_email.setText(user.get("email"));
		txt_telefono.setText(user.get("telefono"));
		
		chk_cambiarPass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					txt_password.setEnabled(true);
					txt_passwordNueva.setEnabled(true);
				}
				if (!isChecked) {
					txt_password.setEnabled(false);
					txt_passwordNueva.setEnabled(false);
				}
				
			}
		});
		
		btn_guardar.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ArrayList<String> params = new ArrayList<String>();
				params.add(txt_nombre.getText().toString());
				params.add(txt_telefono.getText().toString());
				params.add(txt_email.getText().toString());
				if (chk_cambiarPass.isChecked()) {
					params.add(txt_password.getText().toString());
					params.add(txt_passwordNueva.getText().toString());
					params.add("true");
				}
				else{
					params.add("");
					params.add("");
					params.add("false");
				}
				new updateUsuario().execute(params);
				
			}
		});
		

		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_cuenta, menu);
		return true;
	}
	
	@Override
    public boolean onOptionsItemSelected(MenuItem item){
		UserFunctions userFunction= new UserFunctions();
    	switch (item.getItemId()) {
		case R.id.mnu_salir:
			if(userFunction.logoutUser(getApplicationContext())){
				Intent mainActivity=new Intent(getApplicationContext(), MainActivity.class);
				mainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mainActivity);
			}
			
			return true;
			
		case R.id.acercaDe:
			Intent iAcercaDe = new Intent(getApplicationContext(), AcercaDe.class);
			startActivity(iAcercaDe);
			return true;

	default:
		return super.onOptionsItemSelected(item);
    	}
	}
	
	class updateUsuario extends AsyncTask<ArrayList<String>, String, JSONObject>{

		@Override
		public void onPreExecute(){
			super.onPreExecute();
			pDialogo=new ProgressDialog(EditCuenta.this);
			pDialogo.setMessage("Guardando cambios...");
			pDialogo.setIndeterminate(false);
            pDialogo.setCancelable(false);
            pDialogo.show();
		}
		
		@Override
		protected JSONObject doInBackground(ArrayList<String>... params) {
			ArrayList<String> passed = new ArrayList<String>();
			Boolean cambioPass=false;
			passed=params[0];
			if (passed.get(5).compareTo("true")==0) {
				cambioPass=true;
			}
			UserFunctions userFunction=new UserFunctions();
			
			JSONObject json=userFunction.updateUser(getApplicationContext(), passed.get(0), passed.get(1), passed.get(2), passed.get(3), passed.get(4), cambioPass);
			return json;
		}
		
		@Override
		protected void onPostExecute(JSONObject json){
			pDialogo.dismiss();
			UserFunctions userFunction=new UserFunctions();
			String mensaje="";
			try {
				if(json.getString("error").compareTo("0")==0){
					json=json.getJSONObject("user");
					userFunction.logoutUser(getApplicationContext());
					db.addUser(json.getString("name"), json.getString("nick"), json.getString("telefono"), json.getString("email"), json.getString("uid"), json.getString("created_at"));
					Intent i = new Intent(getApplicationContext(), EditCuenta.class);
					startActivity(i);
					EditCuenta.this.finish();
					mensaje="Cambio exitoso";
				}
				else{
					mensaje=json.getString("error_msg");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Toast toast = Toast.makeText(getApplicationContext(), mensaje, Toast.LENGTH_SHORT);
			toast.show();
		}
		
	}

}
